# encoding: utf-8

require 'prawn'
require 'open-uri'

module GenPDF
  include Prawn::Images

  class ImageString
    attr_accessor :read

    def initialize( string )
      self.read = string
    end
  end

  class Badge
    attr_accessor :pdf

    def initialize( string, *args )
      options = args.extract_options!
      format = [ 147.40157480314963, # mm2pt(52)
                 226.7716535433071 ] # mm2pt(80)
      
      doc = Prawn::Document.new page_size: format
      w = doc.bounds.width
      h = doc.bounds.height
      doc.font 'fonts/PTF55F.ttf'
      doc.bounding_box [-20, 175], width: (w + 40), height: 60 do
        doc.text string, size: 20, align: :center
      end
      
      img = ImageString.new options[:image] # FIXME: ugly!

      doc.image img, at: [w/2 - 40, 100], width: 80
      self.pdf = doc.render
    end
  end
end

include GenQR
